﻿package com.sz.ticket.dao.impl;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.sz.common.ICommonDAO;
import com.sz.common.SZUtility;
import com.sz.ticket.dao.ITicketDAO;
import com.sz.common.ConstDefine;

public class TicketDAO extends SqlMapClientDaoSupport implements ITicketDAO{
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	private ICommonDAO commonDAO = null;

	public ICommonDAO getCommonDAO() {
		return commonDAO;
	}

	public void setCommonDAO(ICommonDAO commonDAO) {
		this.commonDAO = commonDAO;
	}

	@SuppressWarnings("unchecked")
	public long addTicket(Map map) throws Exception {
		Object obj = getSqlMapClient().insert("ticket_sqlmap.insert_Ticket", map);
		return obj==null ? 0 : Long.parseLong(obj.toString());
	}
	
	@SuppressWarnings("unchecked")
	public List queryTicket(Map map) throws Exception {		
		List list = getSqlMapClient().queryForList("ticket_sqlmap.query_Ticket", map);
		return addition(list);
	}
	
	@SuppressWarnings("unchecked")
	public long queryTicketCount(Map map) throws Exception {
		Map r = (Map)getSqlMapClient().queryForObject("ticket_sqlmap.query_TicketCount", map);
		return Long.parseLong(r.get("COUNT").toString());
	}
	
	@SuppressWarnings("unchecked")
	public List queryTicketPage(Map map) throws Exception {
		List list = getSqlMapClient().queryForList("ticket_sqlmap.query_TicketPage", map);
		return addition(list);
	}
	
	@SuppressWarnings("unchecked")
	public List queryTicketStatusList() throws Exception {
		return getSqlMapClient().queryForList("ticket_sqlmap.query_TicketStatusList");
	}
	
	/**
	 * TICKET_ID
	 * LOCK_MANAGER 对于解锁指当前订单的锁定者(做校验用)
	 * MYVERSION 订单的VERSION(做校验用)
	 * LOCKFLAG： 0解锁/1锁定
	 */
	@SuppressWarnings("unchecked")
	public String lockTicket(Map map) throws Exception {
		/**锁定
		 * 1. 可以查询到订单
		 * 2. lock_manager字段为空(未被锁定)
		 * 3. version未变
		 * 4. 订单状态不能为：
			 	81	客户取消完成
			 	82 	商家取消完成
			 	83	订单确认失败，已取消
		 */
		
		/**解锁
		 * 1. 可以查询到订单
		 * 2. lock_manager字段不为空(被锁定),且与传入的LOCK_MANAGER相同
		 * 3. version未变
		 * 
		 */
		
		String ticket_id = map.get("TICKET_ID").toString();
		String lock_manager = map.get("LOCK_MANAGER").toString();
		String myversion = map.get("MYVERSION").toString();
		String lockFlag = map.get("LOCKFLAG").toString(); // 1 锁定， 0 解锁
		
		String sql = String.format("select cust_tel, status, lock_manager, version from ticket where ticket_id = %s", ticket_id);
		Map r = (Map)commonDAO.queryForObject(sql);		
		if(r == null) return "查询不到订单";	
		
		String cust_tel = r.get("CUST_TEL").toString(); //作为订单的文本标识
		
		if(lockFlag.equals("1")) { //锁定
			
			if(r.get("LOCK_MANAGER") != null) return cust_tel+"订单已被锁定";
			
			String ts = SZUtility.toString_TimeStamp(r.get("VERSION"));
			if(!myversion.equals(ts)) return cust_tel+"订单发生了更改";
			
			long status = Long.valueOf(r.get("STATUS").toString());
			if(81 == status || 82 == status || 83 == status) {
				return cust_tel+"订单已被取消";
			} else {
				Map param = new HashMap();
				param.put("LOCK_MANAGER", lock_manager);
				param.put("TICKET_ID", ticket_id);
				getSqlMapClient().update("ticket_sqlmap.update_LockManager", param);
				return null;
			}
		} else if(lockFlag.equals("0")){ //解锁
			
			if(r.get("LOCK_MANAGER") == null) return cust_tel+"订单未被锁定";
			if(lock_manager.equals(r.get("LOCK_MANAGER").toString()) == false) return cust_tel+"订单的锁定者已改变";
			
			String ts = SZUtility.toString_TimeStamp(r.get("VERSION"));
			if(!myversion.equals(ts)) return cust_tel+"订单发生了更改";
			
			//更新
			getSqlMapClient().update("ticket_sqlmap.update_LockManagerToNULL", ticket_id);
			return null;
		} else {
			return "未知的操作类型";
		}
	}
	
	/**
	 * 取消订单
	 * @param map
	 * 	TICKET_ID
	 * 	MYVERSION
	 * 	OPERATOR
	 * 	CANCEL_FROM : 1客户/2商家
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String cancelTicket(Map map) throws Exception {
		/**
		 * 1. 可以查询到订单
		 * 2. 订单状态不能为：				
			 	81	客户取消完成
			 	82 	商家取消完成			 	
			 	83	订单确认失败，已取消
		 * 3. version未变
		 */
		
		/***注意：与editTicket、nextStatus 的逻辑很相似，以后如果更改，要考虑editTicket和nextStatus是否也要改 **/
		/******** 例如何时清空锁定者     **/
		
		String ticket_id = map.get("TICKET_ID").toString();
		String myversion = map.get("MYVERSION").toString();		
		String operator = map.get("OPERATOR").toString();
		String from = map.get("CANCEL_FROM").toString(); //发起者 1客户 2商家
		
		String sql = String.format("select cust_tel, status, lock_manager, version from ticket where ticket_id = %s", ticket_id);
		Map r = (Map)commonDAO.queryForObject(sql);		
		if(r == null) return "查询不到订单";	
		
		String cust_tel = r.get("CUST_TEL").toString(); //作为订单的文本标识
		
		long cur_status = Long.valueOf(r.get("STATUS").toString()); //订单当前状态
		if(81 == cur_status) {
			return cust_tel + "订单已被客户取消";
		} else if(82 == cur_status) {
			return cust_tel + "订单已被商家取消";
		} else if(83 == cur_status) {
			return cust_tel + "订单由于确认失败，已经被取消";
		} else {
			//do nothing
		}		
		
		String ts = SZUtility.toString_TimeStamp(r.get("VERSION"));
		if(!myversion.equals(ts)) return cust_tel+"订单发生了更改";
		
		String new_status;		
		if(from.equals(ConstDefine.s_fromCustom)) { //客户取消			
			new_status = "81";
		} else if(from.equals(ConstDefine.s_fromBusiness)) { //商家取消
			new_status = "82";
		} else {
			return "未知的取消操作发起方";
		}
		
		//订单日志
		logTicket(ticket_id);
		
		Map param = new HashMap();
		param.put("TICKET_ID", ticket_id);
		param.put("STATUS", new_status);
		param.put("OPERATOR", operator);
		param.put("__SET_LOCKMANAGER_NULL", map.get("__SET_LOCKMANAGER_NULL")); //若前台传了__SET_LOCKMANAGER_NULL的值， 则清空锁定者
		
		getSqlMapClient().update("ticket_sqlmap.update_Status", param);
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public Map editTicket(Map map) throws Exception {
		/**
		 * 1. 可以查询到订单
		 * 2. 订单状态只能为：
				1	客户下单,待确认	
				11	客户更改,待确认
			 	21	商家更改,待确认			 	
		 * 3. version未变
		 */		
		Map rt = new HashMap();
		rt.put("code", "0");
		rt.put("msg", "");
		
		String ticket_id = map.get("TICKET_ID").toString();
		String myversion = map.get("MYVERSION").toString();		
		String operator = map.get("OPERATOR").toString();
		String from = map.get("FROM").toString(); //发起者 1客户 2商家
		String auto_confirm =  SZUtility.toString(map.get("AUTO_CONFIRM").toString());
		
		String sql = String.format("select cust_tel, status, lock_manager, version, confirm_b, confirm_c, notice_flag from ticket where ticket_id = %s", ticket_id);
		Map r = (Map)commonDAO.queryForObject(sql);		
		if(r == null) {
			rt.put("code", "0");
			rt.put("msg", "查询不到订单");
			return rt;	
		}
		
		String cust_tel = r.get("CUST_TEL").toString(); //作为订单的文本标识
		
		long cur_status = Long.valueOf(r.get("STATUS").toString()); //订单当前状态
		
		
		String cur_confirm_b = SZUtility.toString(r.get("CONFIRM_B"));
		String cur_confirm_c = SZUtility.toString(r.get("CONFIRM_C"));
		
		String ts = SZUtility.toString_TimeStamp(r.get("VERSION"));
		if(!myversion.equals(ts)) {
			rt.put("code", "0");
			rt.put("msg", cust_tel+"订单发生了更改");
			return rt;
		}
		
		//后台允许任何状态的编辑
//		if(81 == cur_status || 82 == cur_status || 83 == cur_status) {
//			rt.put("code", "0");
//			rt.put("msg", cust_tel+"订单已被取消");
//			return rt;
//		}
		
		String new_status;
		Map param = new HashMap();
			
		//清空以下
		param.put("OPERATOR", operator);
		param.put("CONFIRM_B", "");
		param.put("CONFIRM_C", "");
		param.put("CONFIRM_B_MANAGER", "");
		param.put("CONFIRM_C_MANAGER", "");

		param.put("OPERATOR", operator);
		
		if(from.equals(ConstDefine.s_fromBusiness)) { //商家编辑			
			if(auto_confirm.equals("1")) {
				//自动客户确认
				new_status = "99"; //确认完成				
				param.put("CONFIRM_C", "1");
				param.put("CONFIRM_C_MANAGER", operator);
			}
			else {
				new_status = "21"; //商家更改,待确认
			}
			
			param.put("CONFIRM_B", "1");
			param.put("CONFIRM_B_MANAGER", operator);
		} else if(from.equals(ConstDefine.s_fromCustom)) {			
			if(auto_confirm.equals("1")) {
				//自动商家确认
				new_status = "99"; //确认完成				
				param.put("CONFIRM_B", "1");
				param.put("CONFIRM_B_MANAGER", operator);
			}
			else {
				new_status = "11"; //客户更改,待确认
			}
		
			param.put("CONFIRM_C", "1");
			param.put("CONFIRM_C_MANAGER", operator);			
		} else {
			rt.put("code", "0");
			rt.put("msg", "未知的取消操作发起方");
			return rt;
		}		
		
		param.put("TICKET_ID", ticket_id);
		param.put("STATUS", new_status);		
		
		param.put("CARD_NO", map.get("CARD_NO"));
		param.put("DEMAND_TIME", map.get("DEMAND_TIME"));
		param.put("DEMAND_NUM", map.get("DEMAND_NUM"));
		param.put("STAFF_NUM", map.get("STAFF_NUM"));
		param.put("POSITION_FLAG", map.get("POSITION_FLAG"));
		param.put("WINDOW_FLAG", map.get("WINDOW_FLAG"));
		param.put("BABY_FLAG", map.get("BABY_FLAG"));
		param.put("NOSMOKING_FLAG", map.get("NOSMOKING_FLAG"));
		param.put("WC_FLAG", map.get("WC_FLAG"));
		param.put("NOTICE_FLAG", map.get("NOTICE_FLAG"));
		param.put("DEMAND_ADDITION", map.get("DEMAND_ADDITION"));
		
		//确认信息
		param.put("BUSINESS_RESPONSE", map.get("BUSINESS_RESPONSE"));
		param.put("DISCOUNT_DESCRIP", map.get("DISCOUNT_DESCRIP"));
		param.put("COMMENT_TEXT", map.get("COMMENT_TEXT"));
		param.put("CONTACT", map.get("CONTACT"));
		param.put("NEED_DEPOSIT", map.get("NEED_DEPOSIT"));
		
		param.put("BUSINESS_ID", map.get("BUSINESS_ID"));
		
		param.put("CUST_TEL", map.get("CUST_TEL"));
		param.put("CUST_NAME", map.get("CUST_NAME"));
		param.put("CUST_SEX", map.get("CUST_SEX"));
		
		//订单日志
		logTicket(ticket_id);
		
		getSqlMapClient().update("ticket_sqlmap.update_Ticket", param);
		
		rt.put("code", "1");
		rt.put("msg", String.format("订单编号:%s，客户电话:%s", ticket_id, cust_tel));
		return rt;	
	}
	
	/**
	 * 正常流程下订单的下一个状态
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String nextStatus(Map map) throws Exception {
		/**
		 * 1. 可以查询到订单
		 * 2. 订单状态不能为：
			 	81	客户取消完成
			 	82 	商家取消完成
			 	83	订单确认失败，已取消
			 	99	确认完成
			 	(与数据库的配置相对应)
		 * 3. version未变
		 */
		
		/***注意：与cancelTicket、editTicket的逻辑很相似，以后如果更改，要考虑cancelTicket和editTicket是否也要改 **/
		/******** 例如何时清空锁定者     **/
		
		String ticket_id = map.get("TICKET_ID").toString();
		String myversion = map.get("MYVERSION").toString();		
		String operator = map.get("OPERATOR").toString();		
		
		String sql = String.format("select cust_tel, status, lock_manager, version from ticket where ticket_id = %s", ticket_id);
		Map r = (Map)commonDAO.queryForObject(sql);		
		if(r == null) return "查询不到订单";	
		
		String cust_tel = r.get("CUST_TEL").toString(); //作为订单的文本标识
		
		long cur_status = Long.valueOf(r.get("STATUS").toString()); //订单当前状态
		if(81 == cur_status) {
			return cust_tel + "订单已被客户取消，不能继续流程";
		} else if(82 == cur_status) {
			return cust_tel + "订单已被商家取消，不能继续流程";
		} else if(83 == cur_status) {
			return cust_tel + "订单由于确认失败已被取消，不能继续流程";
		} else if(99 == cur_status) {
			return cust_tel + "订单已确认完成，不能继续流程";
		} else {
			// do nothing
		}
		
		String ts = SZUtility.toString_TimeStamp(r.get("VERSION"));
		if(!myversion.equals(ts)) return cust_tel+"订单发生了更改，请先刷新订单信息";
		
		Object obj = getSqlMapClient().queryForObject("ticket_sqlmap.query_nextStatus", String.valueOf(cur_status));
		if(obj == null || ((Map)obj).get("NEXT_STATUS")==null) return "找不到下一状态的配置，当前状态为:" + cur_status;
		
		String next_status = ((Map)obj).get("NEXT_STATUS").toString();
		Map param = new HashMap();
		param.put("TICKET_ID", ticket_id);
		param.put("STATUS", next_status);
		param.put("OPERATOR", operator);
		
		//当商家确认时，需要以下信息
		param.put("BUSINESS_RESPONSE", map.get("BUSINESS_RESPONSE"));
		param.put("DISCOUNT_DESCRIP", map.get("DISCOUNT_DESCRIP"));
		param.put("COMMENT_TEXT", map.get("COMMENT_TEXT"));
		param.put("CONTACT", map.get("CONTACT"));
		param.put("NEED_DEPOSIT", map.get("NEED_DEPOSIT"));
		
		param.put("__SET_LOCKMANAGER_NULL", map.get("__SET_LOCKMANAGER_NULL")); //若前台传了__SET_LOCKMANAGER_NULL的值， 则清空锁定者
		
		//订单日志
		logTicket(ticket_id);
		
		getSqlMapClient().update("ticket_sqlmap.update_Status", param);
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public String confirmfailedTicket(Map map) throws Exception {
		/**
		 * 1. 可以查询到订单
		 * 2. 订单状态不能为：				
			 	81	客户取消完成
			 	82 	商家取消完成			 	
			 	83	订单确认失败，已取消
		 * 3. version未变
		 */
		
		/***注意：与editTicket、nextStatus 的逻辑很相似，以后如果更改，要考虑editTicket和nextStatus是否也要改 **/
		/******** 例如何时清空锁定者     **/
		
		String ticket_id = map.get("TICKET_ID").toString();
		String myversion = map.get("MYVERSION").toString();		
		String operator = map.get("OPERATOR").toString();
		String from = map.get("FAILED_FROM").toString(); //发起者 1客户 2商家
		
		String sql = String.format("select cust_tel, status, lock_manager, version from ticket where ticket_id = %s", ticket_id);
		Map r = (Map)commonDAO.queryForObject(sql);		
		if(r == null) return "查询不到订单";	
		
		String cust_tel = r.get("CUST_TEL").toString(); //作为订单的文本标识
		
		long cur_status = Long.valueOf(r.get("STATUS").toString()); //订单当前状态
		if(81 == cur_status) {
			return cust_tel + "订单已被客户取消";
		} else if(82 == cur_status) {
			return cust_tel + "订单已被商家取消";
		} else if(83 == cur_status) {
			return cust_tel + "订单的状态已经为确认失败";
		} else {
			//do nothing
		}		
		
		String ts = SZUtility.toString_TimeStamp(r.get("VERSION"));
		if(!myversion.equals(ts)) return cust_tel+"订单发生了更改";
		
		String new_status = "83";		
		
		
		Map param = new HashMap();
		param.put("TICKET_ID", ticket_id);
		param.put("STATUS", new_status);
		param.put("OPERATOR", operator);
		param.put("__SET_LOCKMANAGER_NULL", "1");
		
		//目前认为订单只会商家确认失败
		param.put("CONFIRM_B", "-1");
		param.put("CONFIRM_B_MANAGER", operator);
		param.put("CONFIRM_C", "");
		param.put("CONFIRM_C_MANAGER", "");
		
		//订单日志
		logTicket(ticket_id);
		
		getSqlMapClient().update("ticket_sqlmap.update_Ticket", param);
		return null;
	}
	/**
	 * 内部使用
	 * @param list
	 * @return
	 */
	@SuppressWarnings("unchecked")	
	private List addition(List list) {		
		if(list == null) return null;
		
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		for(int n = 0; n < list.size(); ++n) {
			Map map = (Map)list.get(n);
			
			map.put("DEMAND_TIME_STR", format.format(map.get("DEMAND_TIME")));
			
			map.put("CREATE_TIME_STR", SZUtility.toString(map.get("CREATE_TIME")));			
			map.put("LOCK_TIME_STR", SZUtility.toString(map.get("LOCK_TIME")));			
			map.put("VERSION_STR", SZUtility.toString_TimeStamp(map.get("VERSION")));			
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List queryBusinessTickets(Map map) throws Exception {
		List list = getSqlMapClient().queryForList("ticket_sqlmap.query_BusinessTickets", map);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public long queryBusinessTicketsCount(Map map) throws Exception {		
		Map r = (Map)getSqlMapClient().queryForObject("ticket_sqlmap.query_BusinessTicketsCount", map);
		return Long.parseLong(r.get("COUNT").toString());
	}
	
	@SuppressWarnings("unchecked")
	public List queryBusinessTicketsPage(Map map) throws Exception {
		List list = getSqlMapClient().queryForList("ticket_sqlmap.query_BusinessTicketsPage", map);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public void checkTicket_Business(Map map) throws Exception {
		
		//订单日志
		String ticket_id = map.get("TICKET_ID")!=null ? map.get("TICKET_ID").toString() : "";
		if(ticket_id.length() > 0) {
			logTicket(ticket_id);
		}
		
		getSqlMapClient().update("ticket_sqlmap.update_BusinessCheck", map);
	}
	
	@SuppressWarnings("unchecked")
	public void checkTicket_Custom(Map map) throws Exception {
		
		//订单日志
		String ticket_id = map.get("TICKET_ID")!=null ? map.get("TICKET_ID").toString() : "";
		if(ticket_id.length() > 0) {
			logTicket(ticket_id);
		}
		
		getSqlMapClient().update("ticket_sqlmap.update_CustomCheck", map);
	}
	
	@SuppressWarnings("unchecked")
	public Map confirmTicket(Map map) throws Exception {
		/**
		 * 1. 可以查询到订单
		 * 2. 订单状态只能为：
				1	客户下单,待确认	
				11	客户更改,待确认
			 	21	商家更改,待确认			 	
		 * 3. version未变
		 */		
		Map rt = new HashMap();
		rt.put("code", "0");
		rt.put("msg", "");
		
		String ticket_id = map.get("TICKET_ID").toString();
		String myversion = map.get("MYVERSION").toString();		
		String operator = map.get("OPERATOR").toString();
		String from = map.get("FROM").toString(); //发起者 1客户 2商家
		String auto_confirm =  SZUtility.toString(map.get("AUTO_CONFIRM").toString());
		
		String sql = String.format("select cust_tel, status, lock_manager, version, confirm_b, confirm_c, notice_flag from ticket where ticket_id = %s", ticket_id);
		Map r = (Map)commonDAO.queryForObject(sql);		
		if(r == null) {
			rt.put("code", "0");
			rt.put("msg", "查询不到订单");
			return rt;	
		}
		
		String cust_tel = r.get("CUST_TEL").toString(); //作为订单的文本标识
		
		long cur_status = Long.valueOf(r.get("STATUS").toString()); //订单当前状态
		
		
		String cur_confirm_b = SZUtility.toString(r.get("CONFIRM_B"));
		String cur_confirm_c = SZUtility.toString(r.get("CONFIRM_C"));
		
		String ts = SZUtility.toString_TimeStamp(r.get("VERSION"));
		if(!myversion.equals(ts)) {
			rt.put("code", "0");
			rt.put("msg", cust_tel+"订单发生了更改");
			return rt;
		}
		
		if(81 == cur_status || 82 == cur_status || 83 == cur_status) {
			rt.put("code", "0");
			rt.put("msg", cust_tel+"订单已被取消");
			return rt;
		}
		
		String new_status;
		Map param = new HashMap();
		
		param.put("OPERATOR", operator);
		//确认完成的订单 重新确认
		if(99 == cur_status) {			
			cur_status = 11; //认为是客户更改，待确认
			cur_confirm_b = "";
			cur_confirm_c = "";
			from = ConstDefine.s_fromBusiness;
			
			//清空以下			
			param.put("CONFIRM_B", "");
			param.put("CONFIRM_C", "");
			param.put("CONFIRM_B_MANAGER", "");
			param.put("CONFIRM_C_MANAGER", "");
		}
		
		if(from.equals(ConstDefine.s_fromBusiness)) { //商家确认
			if(cur_status==21 && cur_confirm_c.equals("1")==false) {
				//商家更改,待确认 且 用户未确认
				rt.put("code", "0");
				rt.put("msg", cust_tel + "订单当前需要客户先确认");
				return rt;
			}			
			
			if(cur_confirm_c.equals("1")) {
				new_status = "99";				
				param.put("__SET_LOCKMANAGER_NULL", "1"); //确认完成 清空锁定者
			} else {				
				if(auto_confirm.equals("1")) {
					//自动客户确认
					new_status = "99"; //确认完成					
					param.put("__SET_LOCKMANAGER_NULL", "1"); //确认完成 清空锁定者
					
					param.put("CONFIRM_C", "1");
					param.put("CONFIRM_C_MANAGER", operator);
				}
				else {
					new_status = String.valueOf(cur_status); //还是当前状态  等待客户确认
				}
			}
			
			param.put("CONFIRM_B", "1");
			param.put("CONFIRM_B_MANAGER", operator);
		} else if(from.equals(ConstDefine.s_fromCustom)) {			
			if((cur_status==1 || cur_status==11) && cur_confirm_b.equals("1")==false) {
				//客户下单,待确认、客户更改,待确认  且  商家未确认				
				rt.put("code", "0");
				rt.put("msg", cust_tel + "订单当前需要商家先确认");
				return rt;
			}

			if(cur_confirm_b.equals("1")) {
				new_status = "99";				
				param.put("__SET_LOCKMANAGER_NULL", "1"); //确认完成 清空锁定者
			} else {
				if(auto_confirm.equals("1")) {
					//自动商家确认
					new_status = "99"; //确认完成					
					param.put("__SET_LOCKMANAGER_NULL", "1"); //确认完成 清空锁定者
					
					param.put("CONFIRM_B", "1");
					param.put("CONFIRM_B_MANAGER", operator);
				}
				else {
					new_status = String.valueOf(cur_status); //还是当前状态  等待客户确认
				}
			}			
			param.put("CONFIRM_C", "1");
			param.put("CONFIRM_C_MANAGER", operator);			
		} else {
			rt.put("code", "0");
			rt.put("msg", "未知的取消操作发起方");
			return rt;
		}		
		
		param.put("TICKET_ID", ticket_id);
		param.put("STATUS", new_status);		
		
		param.put("CARD_NO", map.get("CARD_NO"));
		param.put("DEMAND_TIME", map.get("DEMAND_TIME"));
		param.put("DEMAND_NUM", map.get("DEMAND_NUM"));
		param.put("STAFF_NUM", map.get("STAFF_NUM"));
		param.put("POSITION_FLAG", map.get("POSITION_FLAG"));
		param.put("WINDOW_FLAG", map.get("WINDOW_FLAG"));
		param.put("BABY_FLAG", map.get("BABY_FLAG"));
		param.put("NOSMOKING_FLAG", map.get("NOSMOKING_FLAG"));
		param.put("WC_FLAG", map.get("WC_FLAG"));
		param.put("NOTICE_FLAG", map.get("NOTICE_FLAG"));
		param.put("DEMAND_ADDITION", map.get("DEMAND_ADDITION"));
		
		//确认信息
		param.put("BUSINESS_RESPONSE", map.get("BUSINESS_RESPONSE"));
		param.put("DISCOUNT_DESCRIP", map.get("DISCOUNT_DESCRIP"));
		param.put("COMMENT_TEXT", map.get("COMMENT_TEXT"));
		param.put("CONTACT", map.get("CONTACT"));
		param.put("NEED_DEPOSIT", map.get("NEED_DEPOSIT"));
		
		param.put("BUSINESS_ID", map.get("BUSINESS_ID"));
		
		param.put("CUST_TEL", map.get("CUST_TEL"));
		param.put("CUST_NAME", map.get("CUST_NAME"));
		param.put("CUST_SEX", map.get("CUST_SEX"));
		
		//订单日志
		logTicket(ticket_id);
		
		getSqlMapClient().update("ticket_sqlmap.update_Ticket", param);
		
		rt.put("code", "1");
		rt.put("msg", String.format("订单编号:%s，客户电话:%s", ticket_id, cust_tel));
		return rt;
	}
	
	@SuppressWarnings("unchecked")
	public List queryBusinessRecommand(Map map) throws Exception {
		return getSqlMapClient().queryForList("ticket_sqlmap.query_BusinessRecomand", map);
	}
	
	@SuppressWarnings("unchecked")
	public List queryTicketLog(Map map) throws Exception {
		//先查询当前的
		List list = queryTicket(map);		
		
		//查历史
		List list_h = getSqlMapClient().queryForList("ticket_sqlmap.query_TicketLog", map);
		list_h = addition(list_h);
		
		list.addAll(list_h);
		return list;		
	}
	
	public void logTicket(String ticket_id) throws Exception {
		if(ticket_id==null || ticket_id.trim().length()==0){
			return;
		}
		String sql = "insert into h_ticket select * from ticket where ticket_id = "+ticket_id;
		getSqlMapClient().insert("common_sqlmap.insert_common", sql);
	}
}
